#include "qpushbuttonandqlael.h"
#include"selectfriendarea.h"
#include"modle/data.h"
#include"privatechatsesiondetail.h"
#include"debug.h"
QPushButtonAndQLael::QPushButtonAndQLael(const QIcon &icon, const QString &text, QWidget *parent)
    :text(text),
    QWidget(parent)
{

    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    layout = new QVBoxLayout();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(5);
    layout->setAlignment(Qt::AlignCenter);
    first = new QPushButton();
    first->setIcon(icon);
    first->setFixedSize(60, 60);  // 设置固定大小
    first->setIconSize(QSize(50, 50));  // 设置图标大小
    first->setStyleSheet(
        "QPushButton {"
        "   border: none;"
        "   border-radius: 8px;"
        "   background: qlineargradient(135deg, #ff6b6b, #ffa36c);"
        "}"
        "QPushButton:hover {"
        "   background: qlineargradient(135deg, #ff9e9e, #ffc99c);"  // 更亮的悬停颜色
        "   border: 2px solid rgba(255, 255, 255, 0.5);"  // 添加白色边框增强效果
        "}"
        "QPushButton:pressed {"
        "   background: qlineargradient(135deg, #ffd1d1, #ffe0c2);"  // 更亮的按下颜色
        "   border: 2px solid rgba(255, 255, 255, 0.8);"  // 更明显的边框
        "}");

    seconde = new QLabel();
    seconde->setAlignment(Qt::AlignLeft);
    QFont font("微软雅黑", 12);
    seconde->setFont(font);
    seconde->setStyleSheet(
        "QLabel {"
        "   color: #e0e0e0;"
        "   background: transparent;"
        "}");
    const int maxsize=60;
    QFontMetrics mertics(font);
    int textwidth=mertics.horizontalAdvance(text);
    if(textwidth>maxsize)
    {
        QString tail="...";
        int tailsize=mertics.horizontalAdvance(tail);
        int truewidth=maxsize-tailsize;
        double rate=(double)truewidth/maxsize;
        int tem=text.size()*rate-1;
        QString truetext=text.left(tem);
        LOG()<<truetext<<" "<<tem<<" "<<rate;
        seconde->setText(truetext+tail);
    }
    else
    {
          seconde->setText(text);
    }
    // 将控件添加到布局
    layout->addWidget(first, 0, Qt::AlignCenter);
    layout->addWidget(seconde, 0, Qt::AlignCenter);
    layout->addStrut(0);
    // 设置布局
    this->setLayout(layout);

    connect(first,&QPushButton::clicked,this,[=](){
        if(this->text=="添加")
        {
            // PrivateChatSesionDetail*parent=dynamic_cast<PrivateChatSesionDetail*>(this->parent());
            // parent->close();
            //LOG()<<"================";
            SelectFriendArea*wid=new SelectFriendArea();
#ifdef TEST_UI
            QIcon *avator=new QIcon(":/Resources/avator1.jpg");
            QString name="张三";
            for(int i=1;i<=20;i++)
            {
                wid->addItem(QString::number(i),*avator,name+QString::number(i),false);
            }
#endif
            wid->exec();
        }
    });
}
